<template>
  <ElSelect filterable clearable :="$attrs" placeholder="" @change="onChange">
    <ElOption v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
  </ElSelect>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { ElSelect, ElOption } from 'element-plus'
import { getDeptNoItems } from '../api'

interface Props {
  data: any;
}
const props = withDefaults(defineProps<Props>(), {
  data: () =>({}),
})

const options = ref<Array<{ label: string, value: string }>>([])

const getOptions = () => {
  if (props.data.companyNo) {
    getDeptNoItems(props.data.companyNo)
      .then(res => {
        options.value = res
      })
  } else {
    options.value = []
  }
}

watch(() => props.data.companyNo, () => {
  getOptions()
})

const onChange = (val) => {
  const o = options.value.find(i => i.value === val) || {} as any
  props.data.deptName = o.label
}

onMounted(() => {
  getOptions()
})
</script>
